import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:wan_android_flutter/common/resources/colors/theme_data_extension.dart';

import '../resources/colors/color_data.dart';
import 'base_app_bar.dart';

abstract class BasePage<T> extends BaseStatelessWidget {
  BasePage({super.key}) {
    _globalKey = GlobalKey<ScaffoldState>();
  }

  late final GlobalKey<ScaffoldState> _globalKey;

  final String? tag = null;

  T get logic => GetInstance().find<T>(tag: tag)!;

  /// 是否展示AppBar标题栏
  bool showAppBar() => true;

  /// 是否展示回退按钮
  bool showLeftButton() => true;

  /// 页面标题设置
  String title() => "";

  /// 标题栏的视图
  Widget? buildTitleWidget() => null;

  /// 创建ActionWidget(最右边视图)
  List<Widget>? buildRightWidgets(BuildContext context) => null;

  /// appBar颜色
  Color? appBarColor() => colorData.bgWhite;

  /// 内容视图
  Widget buildContent(BuildContext context);

  /// 自定义颜色数据扩展
  ColorData get colorData => Theme.of(baseContext).colorData;

  /// AppBar toolbarHeight
  double get toolbarHeight => 52;

  /// 状态栏颜色设置
  SystemUiOverlayStyle systemOverlayStyle(BuildContext context) {
    if (Theme.of(context).brightness == Brightness.dark) {
      return const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarColor: Colors.black,
        systemNavigationBarIconBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.light,
        statusBarBrightness: Brightness.dark,
      );
    } else {
      return const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarColor: Colors.white,
        systemNavigationBarIconBrightness: Brightness.dark,
        statusBarIconBrightness: Brightness.dark,
        statusBarBrightness: Brightness.light,
      );
    }
  }

  PreferredSizeWidget? buildAppBar(BuildContext context) => _buildAppBar(context);

  PreferredSizeWidget? _buildAppBar(BuildContext context) {
    if (showAppBar()) {
      return BaseAppBar(
        /// 标题
        title(),

        /// 状态栏样式
        systemOverlayStyle: systemOverlayStyle(context),

        /// 是否展示左边按钮
        showLeftButton: showLeftButton(),

        /// 标题栏的视图
        titleWidget: buildTitleWidget(),

        /// 最右边视图
        rightWidgets: buildRightWidgets(context),

        /// 背景颜色
        backgroundColor: appBarColor(),

        toolbarHeight: toolbarHeight,
      );
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _globalKey,
      extendBody: false,
      extendBodyBehindAppBar: false,
      appBar: buildAppBar(context),
      body: buildContent(context),
    );
  }
}

/// 自定义StatelessWidget
abstract class BaseStatelessWidget extends Widget {
  final BaseStateContext stateContext = BaseStateContext();

  BaseStatelessWidget({super.key});

  @override
  StatelessElement createElement() => StatelessElement(this);

  @protected
  Widget build(BuildContext context);

  @protected
  buildContext(BuildContext context) {
    stateContext.setBaseContext(context);
  }

  @protected
  BuildContext get baseContext => stateContext.baseContext;
}

class StatelessElement extends ComponentElement {
  StatelessElement(BaseStatelessWidget super.widget);

  late BuildContext _context;

  BuildContext get context => _context;

  @override
  Widget build() {
    _context = this;
    var build = (widget as BaseStatelessWidget);
    build.buildContext(context);
    return build.build(_context);
  }

  @override
  void update(BaseStatelessWidget newWidget) {
    super.update(newWidget);
    assert(widget == newWidget);
    rebuild(force: true);
  }
}

@protected
class BaseStateContext {
  /// 获取上下文
  BuildContext get baseContext => _baseContext;

  /// 上下文
  late BuildContext _baseContext;

  /// 设置上下文
  setBaseContext(BuildContext context) {
    _baseContext = context;
  }
}
